package api.goods;

import bean.Goods;
import com.alibaba.fastjson.JSON;
import dao.GoodsDao;
import utils.Response;
import utils.ResponseCode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class GetGoodsByNamServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Goods> goodsList=null;
        Response<List<Goods>> build=null;
        req.setCharacterEncoding("utf-8");
        String name=req.getParameter("name");
        try {
            goodsList = GoodsDao.findByName(name);
            build = Response.build(ResponseCode.SUCCESS, goodsList);
        }catch (SQLException e){
            e.printStackTrace();
            build = Response.build(ResponseCode.ERROR, goodsList);
        }
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSON.toJSON(build).toString());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
